<?php

namespace app\index\model;

use think\Model;

class Assetsversions extends Model
{
    protected $hidden = ['id','assets_id','file_md5'];
    protected $autoWriteTimestamp = 'datetime';

    public static function get_data($data='')
    {
        return self::where($data)->order('id desc')->select();
    }

    public static function get_count($data='')
    {
        return self::where($data)->count();
    }

    public static function add_data()
    {
        $data_count = self::where(['assets_versions_num'=>input('assets_versions_num'),'assets_id'=>input('assets_id')])->count() ;
        if(!$data_count){
            $data =input() ;
            // 根据资产id 在Asset_to_shot查询对应镜头id
            $get_data =Asset_to_shot::where(['assets_id'=>$data['assets_id']])->distinct(true)->field('shot_id')->select()->toArray();

            foreach ($get_data as $key => $value) {
                // 然后修改镜头表状态
                Shot::where(['id'=>$value['shot_id']])->update(['status'=>1]) ;
            }
            $data['create_time'] = date("Y-m-d H:i:s") ; // 
            Assets::where(['id'=>$data['assets_id']])->update(['status'=>1]) ;
            //增加版本表数据 并返回 
            return [1 , self::strict(false)->insert($data)];
        }else{
            return [0 , "data is repeat"];
        }
    }

    public static function update_data($data)
    {
        return self::update($data);
    }

     public static function delete_data($data='')
    {
        return self::where($data)->delete();
    }
}